﻿using System;
using System.Threading;
using NHibernate.Event;
using NHibernate.Event.Default;
using EasyCMMS.Services.Domain;

namespace EasyCMMS.Services.Repositories
{
    public class CustomSaveEventListener : DefaultSaveEventListener
    {

        protected override object PerformSaveOrUpdate(SaveOrUpdateEvent evt)
        {
            IEntity entity = evt.Entity as IEntity;
            if (entity != null)
                ProcessEntityBeforeInsert(entity);

            return base.PerformSaveOrUpdate(evt);
        }

        internal virtual void ProcessEntityBeforeInsert(IEntity entity)
        {
            entity.CreatedBy = Thread.CurrentPrincipal.Identity.Name;
            entity.CreatedAt = DateTime.Now;
            entity.ModifiedAt = entity.CreatedAt;
            entity.ModifiedBy = entity.CreatedBy;
        }
    }
}
